package com.enthusiasm.view;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.view.MotionEvent;

import com.enthusiasm.constants.AbstractSurfaceView;
import com.enthusiasm.constants.BitmapManager;
import com.enthusiasm.constants.SoundManager;
import com.enthusiasm.engine.MyBitmap;
import com.enthusiasm.engine.MyButton;
import com.enthusiasm.engine.MyRect;
import com.enthusiasm.frame.GameModeActivity;
import com.enthusiasm.frame.GameModeThreeActivity;
import com.game.PuzzleActivity;

public class GameModeThreeView extends AbstractSurfaceView{
	private MyRect screenRect;
	private MyRect backRect;
	private ArrayList<MyRect> levelRect;
	private Bitmap bmpBackground;
	private Bitmap bmpBack01;
	private Bitmap bmpBack02;
	private Bitmap bmpLevelBg;
	private Bitmap bmpLevelLock;
	private MyBitmap bmpLevelNum;
	private MyButton back;
	private int currentRecord = 0;

	public GameModeThreeView(Context context) {
		super(context);
		SharedPreferences sp = context.getSharedPreferences("gameRecord", Context.MODE_PRIVATE);
		currentRecord = sp.getInt("modethree", 0);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		back.onTouchEvent(event);
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			for(int i = 0; i<levelRect.size(); i++){
				 if(levelRect.get(i).contains(event.getX(),event.getY())){
					if(i <= currentRecord){
						Intent intent  = new Intent();
						intent.putExtra("bigstage", 2);
						intent.putExtra("smallstage", i);
						intent.setClass(context,PuzzleActivity.class);
						context.startActivity(intent);
						((GameModeThreeActivity)context).finish();
						((GameModeThreeActivity)context).overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
					}
				}
			}
		}
		return true;
	}

	@Override
	public void draw() {
		canvas.drawBitmap(bmpBackground, null, screenRect, paint);
		back.draw(canvas, paint);
		for(int i=0; i< levelRect.size(); i++){
			canvas.drawBitmap(bmpLevelBg, null, levelRect.get(i),paint);
		}
		for(int i = 0; i < levelRect.size(); i++){
			MyRect rect = levelRect.get(i);
			if(i <= currentRecord){
				if(i < 9 )
					bmpLevelNum.drawImageInRect(canvas, rect, 0, i+1);
				else
					bmpLevelNum.drawImageInRect(canvas, rect, (i+1)/10, (i+1)%10);
			}
			else
				canvas.drawBitmap(bmpLevelLock, null,rect, paint);
		}
	}

	@Override
	public void logic() {
	}

	@Override
	public void initBitmap() {
		BitmapManager.getInstance(context).initMode03Bitmap();
		bmpBackground = BitmapManager.getInstance(context).bmpScreen03;
		bmpBack01 = BitmapManager.getInstance(context).bmpBack01;
		bmpBack02 = BitmapManager.getInstance(context).bmpBack02;
		back = new MyButton(bmpBack01,bmpBack02) {
			@Override
			public void doClick() {
				SoundManager.getInstance(context).playSoundClick();
				Intent intent  = new Intent(context,GameModeActivity.class);
				intent.putExtra("position", 3);
				context.startActivity(intent);
				((GameModeThreeActivity)context).finish();
				((GameModeThreeActivity)context).overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
			}
		};
		bmpLevelBg = BitmapManager.getInstance(context).bmpLevelBg;
		bmpLevelLock = BitmapManager.getInstance(context).bmpLevelLock;
		bmpLevelNum = new MyBitmap(BitmapManager.getInstance(context).bmpLevelNum);
	}

	@Override
	public void initRect() {
		screenRect = new MyRect(0, 0, screenW, screenH);
		backRect = new MyRect(screenW/40, screenH/30, screenW/15,screenH/9);
		back.setRect(backRect);
		levelRect = new ArrayList<MyRect>();
		for(int i = 0; i<5; i++){
			MyRect level = new MyRect(screenW/8+i*screenW/6, screenH/20, screenW/10, screenH/6);
			levelRect.add(level);
		}
		for(int i = 0; i<5; i++){
			MyRect level = new MyRect(screenW/10+i*screenW/6, screenH/20+screenH/4, screenW/10, screenH/6);
			levelRect.add(level);
		}
		for(int i = 0; i<5; i++){
			MyRect level = new MyRect(screenW/8+i*screenW/6, screenH/20+screenH/2, screenW/10, screenH/6);
			levelRect.add(level);
		}
	}

	@Override
	public void releaseBitmap() {
		BitmapManager.getInstance(context).releaseMode03Bitmap();
	}
}

